package com.zhenyi.youqu.core;

import javax.servlet.http.HttpServletRequest;

import org.hashids.Hashids;

import com.jfinal.kit.LogKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbPro;
import com.jfinal.plugin.activerecord.Page;
import com.youqu.common.model.YqApp;
import com.zhenyi.core.ZIndexController;

import cn.hutool.core.util.PageUtil;

public class YQController extends ZIndexController {

	protected final String YQ_APP_ID = PropKit.get("YQ_APP_ID", "");
	protected final String YQ_APP_SECRET = PropKit.get("YQ_APP_SECRET", "");

	// protected final String PARTNER = PropKit.get("YQ_PARTNER", "");
	// protected final String NOTIFY_URL = PropKit.get("YQ_NOTIFY_URL", "");
	// protected final String PARTNER_KEY = PropKit.get("YQ_PARTNER_KEY", "");
	// protected final String REFUND_NOTIFY_URL =
	// PropKit.get("YQ_REFUND_NOTIFY_URL", "");
	// protected final String SSL_PATH = PathUtils.getFile("cert", APP_ID,
	// "apiclient_cert.p12");
	// protected final PayUtils payUtils = new PayUtils(APP_ID, PARTNER,
	// PARTNER_KEY, SSL_PATH);

	protected final Hashids HASHID = new Hashids("youqu", 8);
	// protected final Hashids HASH_IDS = new Hashids("youqu", 8);
	/**
	 * 有趣工具库
	 */
	protected final YQUtils yqUtils = new YQUtils();

	protected YqApp getApp() {
		Long appId = getAppId();
		return YqApp.dao.findByAppId(appId);
	}

	/**
	 * 通过appId的code换appId
	 * 
	 * @return
	 */
	protected Long getAppId() {
		String code = getPara("appId");
		if (StrKit.isBlank(code)) {
			return null;
		}
		try {
			Long appId = new Hashids("zhenyi", 8).decode(code)[0];
			return appId;
		} catch (Exception e) {
			LogKit.warn("get appId fail: " + code);
			return null;
		}
	}

	protected DbPro db = Db.use("youqu");

	protected Integer getPageSize() {
		int pageSize = getParaToInt("pageSize", PAGE_SIZE);
		setAttr("pageSize", pageSize);
		return pageSize;
	}

}
